package com.gateway.plugin.resilience4j.factory;

import io.github.resilience4j.circuitbreaker.CircuitBreaker;
import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig;
import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry;
import io.github.resilience4j.ratelimiter.RateLimiter;
import io.github.resilience4j.ratelimiter.RateLimiterConfig;
import io.github.resilience4j.ratelimiter.RateLimiterRegistry;

/**
 * @program: api-gateway
 * @description:
 * @author: YuKai Fan
 * @create: 2025/2/24 10:04
 **/
public class Resilience4JRegistryFactory {

    private static final RateLimiterRegistry RATE_LIMITER_REGISTRY = RateLimiterRegistry.ofDefaults();

    private static final CircuitBreakerRegistry CIRCUIT_BREAKER_REGISTRY = CircuitBreakerRegistry.ofDefaults();

    private Resilience4JRegistryFactory() {
    }

    /**
     * circuitBreaker
     * @param id
     * @param circuitBreakerConfig
     * @return
     */
    public static CircuitBreaker circuitBreaker(final String id, final CircuitBreakerConfig circuitBreakerConfig) {
        return CIRCUIT_BREAKER_REGISTRY.circuitBreaker(id, circuitBreakerConfig);
    }

    /**
     * rateLimiter.
     *
     * @param id                the id
     * @param rateLimiterConfig the rate limiter config
     * @return RateLimiter
     */
    public static RateLimiter rateLimiter(final String id, final RateLimiterConfig rateLimiterConfig) {
        return RATE_LIMITER_REGISTRY.rateLimiter(id, rateLimiterConfig);
    }

    /**
     * remove.
     *
     * @param id the id
     */
    public static void remove(final String id) {
        CIRCUIT_BREAKER_REGISTRY.remove(id);
        RATE_LIMITER_REGISTRY.remove(id);
    }
}
